<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class It_email_configuration extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		$this->load->model('app_model', 'App');	
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Email Configuration";
		$data['data'] = $this->App->EmailConfig();
		$data['mode'] = "edit";
		viewByTemplate('it_email_configuration/index', $data, 'form');
	}
	
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		

		
		// Validation
		if (post("protocol") == '') {
			$error->Add("protocol", "Protocol", $tpl_empty);
		}
		
		if (post("smtp_host") == '') {
			$error->Add("smtp_host", "SMTP Host", $tpl_empty);
		}
		
		if (post("smtp_port") == '') {
			$error->Add("smtp_port", "SMTP Port", $tpl_empty);
		}
		
		if (post("smtp_timeout") == '') {
			$error->Add("smtp_timeout", "SMTP Timeout", $tpl_empty);
		}
		
		if (post("smtp_user") == '') {
			$error->Add("smtp_user", "SMTP User", $tpl_empty);
		}
		
		if (post("smtp_pass") == '') {
			$error->Add("smtp_pass", "SMTP Password", $tpl_empty);
		}
		
		return $error;
	}
	
	function sendemail(){
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				$this->load->library('email');
				
				$config['protocol']		=post('protocol');
				$config['smtp_host']	=post('smtp_host');
				$config['smtp_port']	=post('smtp_port');
				$config['smtp_timeout']	=post('smtp_timeout');
				$config['smtp_user']	=post('smtp_user');
				$config['smtp_pass']	=post('smtp_pass');
				$config['charset']		='utf-8';  
				$config['newline']		="\r\n";  
				$config['mailtype'] 	= 'html';
		
				$this->email->initialize($config);
				$this->email->from(post('smtp_user'),'ADMINISTRATOR EBUDGET');
			
				$this->email->to(post('to'));
				
				$this->email->cc(post('cc'));
			
				$this->email->subject(post('subject'));
				
				$this->email->message(post('message'));

				$ls_response=@$this->email->send()?'Message was sent successfully!':'Error sending email!';
					
					
							
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	
	}
	
	
	
	function ajax_post(){
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				$this->load->library('email');
				
				$arr_data['config_value']		=post('protocol');
				$this->db->update('it_config', $arr_data, array('config_name'=>'email_protocol'));
				
				$arr_data['config_value']		=post('smtp_host');
				$this->db->update('it_config', $arr_data, array('config_name'=>'smtp_host'));
				
				$arr_data['config_value']		=post('smtp_port');
				$this->db->update('it_config', $arr_data, array('config_name'=>'smtp_port'));
				
				$arr_data['config_value']		=post('smtp_timeout');
				$this->db->update('it_config', $arr_data, array('config_name'=>'smtp_timeout'));
				
				$arr_data['config_value']		=post('smtp_user');
				$this->db->update('it_config', $arr_data, array('config_name'=>'smtp_user'));
				
				$arr_data['config_value']		=encrypt(post('smtp_pass'),$this->config->item('encryption_key'));
				$this->db->update('it_config', $arr_data, array('config_name'=>'smtp_pass'));
				
				
				$tpl_update		= $this->MessageTemplate("CS002");
				$ls_response 	= $tpl_update['message'];
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	
	}
}
?>